//SUMMARY:  This do file merges and cleans survey data from the BSA for our "pre-treatment" test in Ballard-Rosa, Malik, Rickard & Scheve (2021)

clear

//Set directory
cd "C:\Users\cambr\Dropbox\Conjoint Taxation\UK study\AuthoritarianDraft\Submissions\CPS\Replication materials\"

use "Data\BSA data\1993 Original BSA Data.dta" 

keep sector trad stif death obey wrong censor

save "Data\BSA data\1993 Key Variables Only.dta", replace

clear

use "Data\BSA data\1994 Original BSA Data.dta" 

keep sector tradval stif death obey wrong censor

save "Data\BSA data\1994 Key Variables Only.dta", replace

append using  "Data\BSA data\1993 Key Variables Only.dta"

split sector, p(" ")

gen newsector=sector1

replace newsector="B45" if sector=="B 45 8"
replace newsector="B31" if sector=="B 31 2"
replace newsector="B72" if sector=="B 72 1"
replace newsector="B64" if sector=="B 64 6"
replace newsector="B34" if sector=="B 34 6"
replace newsector="B92" if sector=="B 92 8"
replace newsector="B42" if sector=="B 42 1"

drop if sector=="XXXXXX"

replace newsector="E16" if sector=="E 16 4"
replace newsector="E18" if sector=="E 18 2"
replace newsector="E4" if sector=="E 4  9"
replace newsector="E6" if sector=="E 6  3"

replace newsector="G74" if sector=="G 74 5"
replace newsector="G75" if sector=="G 75 8"
replace newsector="G64" if sector=="G 64 2"
replace newsector="G15" if sector=="G 15 6"
replace newsector="G3" if sector=="G 3  8"
replace newsector="G43" if sector=="G 43 2"
replace newsector="G12" if sector=="G 12 0"

replace newsector="L41" if sector=="L 41 3"
replace newsector="L47" if sector=="L 47 5"
replace newsector="L19" if sector=="L 19 4"

replace newsector="M11" if sector=="M 11 2"
replace newsector="M24" if sector=="M 24 2"
replace newsector="M31" if sector=="M 31 1"

replace newsector="N9" if sector=="N 9  0"
replace newsector="N18" if sector=="N 18 2"

replace newsector="S63" if sector=="S 63 6"
replace newsector="S72" if sector=="S 72 8"
replace newsector="S61" if sector=="S 61 1"

replace newsector="W3" if sector=="W 3  6"
replace newsector="W10" if sector=="W 10 4"

rename sector oldsector

rename newsector sector

drop sector1 sector2 sector3 sector4 oldsector

rename censor censor_9394
rename tradvals tradvals_9394
rename stifsent stifsent_9394
rename deathapp deathapp_9394
rename obey obey_9394
rename wronglaw wronglaw_9394

//This recodes missing & no response answers to ".", then recodes answers to be increasing in authoritarian values
* "Young people today don't have enough respect for traditional British values."
replace tradvals=. if tradvals==-1
replace tradvals=. if tradvals==8
replace tradvals=. if tradvals==9
recode tradvals (1=5) (2=4) (3=3) (4=2) (5=1)
* "People who break the law should be given stiffer sentences."
replace stifsent=. if stifsent==-1
replace stifsent=. if stifsent==8
replace stifsent=. if stifsent==9
recode stifsent (1=5) (2=4) (3=3) (4=2) (5=1)
* "For some crimes, the death penalty is the most appropriate sentence."
replace death=. if death==-1
replace death=. if death==8
replace death=. if death==9
recode death (1=5) (2=4) (3=3) (4=2) (5=1)
* "Schools should teach children to obey authority."
replace obey=. if obey==-1
replace obey=. if obey==8
replace obey=. if obey==9
recode obey (1=5) (2=4) (3=3) (4=2) (5=1)
* "The law should always be obeyed even if a particular law is wrong."
replace wrong=. if wrong==-1
replace wrong=. if wrong==8
replace wrong=. if wrong==9
recode wrong (1=5) (2=4) (3=3) (4=2) (5=1)
* "Censorship of films and magazines is necessary to uphold moral standards."
replace censor=. if censor==-1
replace censor=. if censor==8
replace censor=. if censor==9
recode censor (1=5) (2=4) (3=3) (4=2) (5=1)

save "Data\BSA data\Pooled 1993 and 1994 Key Variables.dta", replace

clear

//Next need to match from postcode to TTWA
use "Data\Regional data\Postcode to TTWA1991 (cleaned).dta" 

keep postcode TTWA1991_name1 TTWA1991_code1

rename postcode sector

sort sector

save "Data\BSA data\TTWA to merge.dta", replace

//This merges on our survey data
use "Data\UK full survey (indiv level).dta", clear

keep postcode_outward TTWA1991_code1 ipw_1991_2000_weighted ipw_1991_2007_weighted

save "Data\BSA data\ipw working file.dta", replace

rename postcode_outward sector

collapse (mean) ipw_1991_2000_weighted ipw_1991_2007_weighted, by(sector) cw

sort sector

save "Data\BSA data\postcode ipw to merge.dta", replace

clear

use "Data\BSA data\ipw working file.dta"

collapse (mean) ipw_1991_2000_weighted ipw_1991_2007_weighted, by(TTWA1991_code1) cw

sort TTWA1991_code1

save "Data\BSA data\ttwa ipw to merge.dta", replace

clear

use "Data\BSA data\Pooled 1993 and 1994 Key Variables.dta", clear

sort sector

merge m:1 sector using "Data\BSA data\TTWA to merge.dta"
drop if _merge!=3
drop _merge

collapse (mean) tradvals stifsent deathapp obey wronglaw censor, by( TTWA1991_code1 ) cw

sort TTWA1991_code1

merge 1:1 TTWA1991_code1 using  "Data\BSA data\ttwa ipw to merge.dta"
drop if _merge!=3
drop _merge 

save "Data\BSA data\Mean Values 1993 and 1994 by TTWA code.dta", replace

//Now perform difference in means, by top and bottom quartile, for pre-treatment authoritarian values
xtile pct = ipw_1991_2007_weighted , n(4)
tab pct, gen(iq)

gen highLow = 0 if iq4==1
replace highLow = 1 if iq1==1

ttest tradvals_9394, by(highLow)
ttest stifsent_9394 , by(highLow)
ttest deathapp_9394  , by(highLow)
ttest obey_9394 , by(highLow)
ttest wronglaw_9394 , by(highLow)
ttest censor_9394, by(highLow)
